package com.fandou.base.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.fandou.base.R;

public class LoadingView {
    private ImageView IV_loading;
    private Dialog mDialog;
    private TextView tv_msg;
    private boolean mCancelAble = true;

    public LoadingView(Context context) {
        mDialog = new Dialog(context, R.style.style_loading_view);
        View contentView = View.inflate(context, R.layout.loading_view_layout, null);
        mDialog.setContentView(contentView);
        IV_loading = mDialog.findViewById(R.id.iv_pop_loading_load);
        tv_msg = mDialog.findViewById(R.id.tv_msg);
        // mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        mDialog.setCanceledOnTouchOutside(false);
    }

    public void loading() {
        // if (mDialog != null && !mDialog.isShowing()) {
        // mDialog.setCancelable(true);
        // tv_msg.setText("");
        // ((AnimationDrawable) IV_loading.getDrawable()).start();
        // mDialog.show();
        // }
        this.loading("正在努力加载中...");
        mCancelAble = true;
        mDialog.setCancelable(mCancelAble);
    }

    public void loading(String text) {
        if (mDialog != null && !mDialog.isShowing()) {
            mDialog.setCancelable(true);
            mCancelAble = true;
            tv_msg.setText(text);
            ((AnimationDrawable) IV_loading.getDrawable()).start();
            mDialog.show();

            mCancelAble = true;
            mDialog.setCancelable(mCancelAble);
        } else {
            tv_msg.setText(text);
        }
    }

    public void loadingNoCancel() {
        // if (mDialog != null && !mDialog.isShowing()) {
        // mDialog.setCancelable(false);
        // ((AnimationDrawable) IV_loading.getDrawable()).start();
        // mDialog.show();
        // }
        this.loadingNoCancel("正在努力加载中...");
        mCancelAble = false;
        mDialog.setCancelable(mCancelAble);
    }

    public void loadingNoCancel(String text) {
        if (mDialog != null && !mDialog.isShowing()) {
            mCancelAble = false;
            mDialog.setCancelable(mCancelAble);
            tv_msg.setText(text);
            ((AnimationDrawable) IV_loading.getDrawable()).start();
            mDialog.show();
        } else {
            tv_msg.setText(text);
        }
    }

    public void endLoading() {
        try {
            if (mDialog != null && mDialog.isShowing()) {
                mDialog.dismiss();
                ((AnimationDrawable) IV_loading.getDrawable()).stop();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public boolean isLoading() {
        if (mDialog != null && mDialog.isShowing()) {
            return true;
        }
        return false;
    }

    public void setOnDismissListener(OnDismissListener listener) {
        mDialog.setOnDismissListener(listener);
    }

    public void setOnKeyDownListener(OnKeyListener listener) {
        mDialog.setOnKeyListener(listener);
    }

    public void recycle() {
        try {
            endLoading();
            IV_loading.clearAnimation();
            IV_loading.setImageBitmap(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isCancelAble() {
        return mCancelAble;
    }

}
